use std::process::Command;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DistroError {
    #[error("无法检测发行版")]
    DetectionFailed,
}

pub enum Distro {
    Debian,
    Arch,
    Fedora,
    OpenSUSE,
    Unknown,
}

impl Distro {
    pub fn detect() -> Result<Self, DistroError> {
        let output = Command::new("lsb_release")
            .arg("-si")
            .output()
            .map_err(|_| DistroError::DetectionFailed)?;
        
        let id = String::from_utf8_lossy(&output.stdout).trim().to_lowercase();
        Ok(match id.as_str() {
            "debian" | "ubuntu" => Self::Debian,
            "arch" => Self::Arch,
            "fedora" => Self::Fedora,
            "opensuse" => Self::OpenSUSE,
            _ => Self::Unknown,
        })
    }
}